package com.infomanage.dao;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.infomanage.bean.FieldBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.exception.DaoException;
import com.infomanage.po.Department;
import com.infomanage.po.HibernateSessionFactory;
import com.infomanage.po.Person;
import com.infomanage.po.PersonDegreeInfo;
import com.infomanage.po.PositionPlan;
import com.infomanage.vo.PersonItem;

public class PersonDAO extends BaseDAO<Person>{
	public PersonItem find(int id) throws DaoException{
		try {
			SearchBean searchBean  = new SearchBean();
			FieldBean fieldBean  = new FieldBean();
			fieldBean.setName("personId");
			fieldBean.setValue(id);
			searchBean.add(fieldBean);
			List result = super.findByFields(new Class[] { Department.class},
					Person.class, new String[] { "deptId"},
					new String[] { "deptId"}, searchBean);
			List<PersonItem> items = parseResult(result);
			return items.size() == 0 ? null : items.get(0);
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	@SuppressWarnings("unchecked")
	public List<PersonItem> findAll() throws DaoException {
		try {
			List result = findAllMutilTable(new Class[] { Department.class},
					Person.class, new String[] { "deptId"},
					new String[] { "deptId"});
			return parseResult(result);
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<PersonItem> findByFields(SearchBean searchBean) throws DaoException {
		List result = super.findByFields(new Class[] { Department.class},
				Person.class, new String[] { "deptId"},
				new String[] { "deptId"}, searchBean);
		return parseResult(result);
	}
	
	public List<PersonItem> parseResult(List result){
		Iterator it = result.iterator();
		List<PersonItem> list = new ArrayList<PersonItem>();
		while (it.hasNext()) {
			Object[] o = (Object[]) it.next();
			Person person = (Person) o[0];
			Department dept = (Department) o[1];
			PersonItem item = new PersonItem();
			item.setBean(person);
			item.setDept(dept);
			list.add(item);
		}
		return list;
	}
	
	public List<Person> findByDept(int deptId) throws DaoException{
		Session session = HibernateSessionFactory.getSession();
		try {
			Query query =  session.createQuery("from "+Person.class.getSimpleName() + " where deptId=?");
			query.setInteger(0, deptId);
			return query.list();
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	
	public int addAndReturnId(Person info) throws DaoException{
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		int id = -1;
		try {
			tx = session.beginTransaction();
			id = (Integer) session.save(info);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return id;
	}
	
	public int count(SearchBean searchBean){
		return super.count(new Class[] { Department.class},
				Person.class, new String[] { "deptId"},
				new String[] { "deptId"}, searchBean);
	}
}
